package Dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import Entity.Train;
import Util.JdbcUtil;

public class TrainDao {
   
	 private Connection conn;
	 
	 public List<Train> selectAll(){
		 List<Train> list = new ArrayList<Train>();
		 conn = JdbcUtil.getConnection();
		 QueryRunner qr = new QueryRunner();
		 String sql = "select * from train_info";
		 
		 try {
			list = qr.query(conn, sql, new BeanListHandler<Train>(Train.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.closeConnection(conn);
		}
		 return list; 
	 }
	 
	 public Train selectTrainById(String no) {
		 Train train = null;
		 conn = JdbcUtil.getConnection();
		 QueryRunner qr = new QueryRunner();
		 String sql = "select * from train_info where train_no=?";
		 try {
			train = qr.query(conn, sql, new BeanHandler<Train>(Train.class),no);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.closeConnection(conn);
		}
		return train;	 
	 }
	 
	 public List<Train> deletTrainById(String no){
		 List<Train> list = new ArrayList<Train>();
		 conn = JdbcUtil.getConnection();
		 QueryRunner qr = new QueryRunner();
		 String sql = "delete * from train_info where train_no=?";
		 try {
		    list = qr.query(conn, sql, new BeanListHandler<Train>(Train.class),no);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.closeConnection(conn);
		}
		return list;
	 }
	 
}
